package com.traffic.util;

import com.traffic.entity.Param;
import com.traffic.service.ParamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Component
@Slf4j
public class CalculateUtil {

    @Autowired
    private ParamService paramService;

    //风险得分
    public Double CalculateRiskScore(List<Double> data, List<Param> params, int n,int start){

        Double sum =new Double(0);
        for(int i=0;i<n;i++){
            sum = sum +Math.abs(data.get(i)*params.get(start+i).getValue());
        }
        return sum;
    }

    public List<Double> SingleScore(List<Double> data, List<Param> params, int n,int start){

        List<Double> singles = new ArrayList<>();

        for(int i=0;i<n;i++){
            singles.add(Math.abs(data.get(i)*params.get(start+i).getValue()));
        }
        return singles;

    }

    public Double SigmoidFunction(Double x){
        return 1.0/(1+Math.exp(-1*x));
    }

    //里程换算等级

    public int CalculateLevel(Double mileage){

        List<Param> levelParam = paramService.queryTargetParam("level");

        Float a = levelParam.get(0).getValue();
        //Integer aa =a.intValue();

        int level = 0;

        int target = mileage.intValue();

        Float resmile = a*(15*(level*level)+75*level-40);

        while(resmile <= target){
            level++;
            resmile = a*(15*(level*level)+75*level-40);
        }

        return level-1;
    }

    //下一等级所需里程
    public int NextLevel(Integer level){

        List<Param> levelParam = paramService.queryTargetParam("level");

        Float a = levelParam.get(0).getValue();
        Integer aa =a.intValue();


        int mile = aa*(15*(level*level)+75*level-40);


        return mile;
    }

    //判断是否在某个时间段内
    public static boolean belongCalendar(Date nowTime, Date beginTime,
                                         Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

}
